package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetRadioAlbumProgramBean;
import com.rjwh.dingdong.client.bean.localbean.GetRadioAlbumProgramBean;
import com.rjwh.dingdong.client.bean.localbean.RadioAnchorInfo;
import com.rjwh.dingdong.client.bean.localbean.Song;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.RadioAlbumProgramModel;
import com.rjwh.dingdong.client.mvp.presenter.IRadioAlbumProgramPresenter;
import com.rjwh.dingdong.client.mvp.view.RadioAlbumProgramView;
import com.rjwh.dingdong.client.service.PlaybackService;

import java.util.List;

/**
 * @author Kay
 * @ClassName RadioAlbumProgramPresenter
 * @Description
 * @date 2016-10-27
 * @history 2016-10-27 author: description:
 */

public class RadioAlbumProgramPresenter extends BasePresenter<RadioAlbumProgramView> implements IRadioAlbumProgramPresenter {

    private RadioAlbumProgramModel model;
    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;


    public RadioAlbumProgramPresenter(Context context, RadioAlbumProgramView mMvpView) {
        super(context, mMvpView);
        model = new RadioAlbumProgramModel(this);
    }

    /**
     * 获取专辑主页(故事列表)
     *
     * @param radioid
     * @param albumid
     */
    @Override
    public void doGetRadioAlbumProgramList(String radioid, String albumid) {
        if (isViewAttached()) {
            model.doGetRadioAlbumProgramList(radioid, albumid);
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETRADIOALBUMPROGRAMLIST:
                    ResGetRadioAlbumProgramBean resGetRadioAlbumProgramBean = (ResGetRadioAlbumProgramBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetRadioAlbumProgramBean != null && resGetRadioAlbumProgramBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetRadioAlbumProgramBean data = resGetRadioAlbumProgramBean.getData();
                        RadioAnchorInfo basicinfo = data.getBasicinfo();
                        List<Song> programlist = data.getProgramlist();

                        if (basicinfo != null && programlist != null) {
                            getMvpView().loadData(basicinfo, programlist);
                        } else {
                            getMvpView().showErrorView();
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            getMvpView().onPlaybackServiceBound(mPlaybackService);
            if(mPlaybackService!=null){
                //绑定服务成功以后去刷新列表中的状态
                getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
                getMvpView().updatePlayToggle(mPlaybackService.getPlayStatus());
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    private void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    private void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    public void subscribe() {
        bindPlaybackService();
        if (mPlaybackService != null && mPlaybackService.isPlaying()) {
            getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
        } else {
            // - load last play list/folder/song
        }
    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        model.detachView();
        detachViews();
    }

}
